package com.bds.portal.action;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bds.model.Hosts;
import com.bds.model.Project;
import com.bds.model.Result;
import com.bds.portal.common.result.Page;
import com.bds.portal.common.result.USession;
import com.bds.portal.service.HostService;
import com.bds.portal.service.ProjectService;
import com.bds.portal.util.Const;

@Controller
@RequestMapping("/action/my/h")
public class HostsAction extends BaseAction {
	private static Logger logger = Logger.getLogger(HostsAction.class);

	@Resource
	private HostService hostService;

	@Resource
	private ProjectService projectService;

	@RequestMapping(value = "/toHost")
	public ModelAndView getHostList(HttpServletRequest request, HttpServletResponse response, Long pid)
			throws Exception {

		ModelAndView view = new ModelAndView();
		try {
			if (pid == null) {
				Project p = projectService.getProject();
				if (p != null) {
					pid = p.getId();
				}
			}
			view.addObject("pid", pid);
			view.setViewName("/web/my/snatch/hosts_list.jsp");

		} catch (Exception e) {
			logger.error("", e);
			return null;
		}
		return view;
	}

	@RequestMapping(value = "/getHostList")
	@ResponseBody
	public Page getHostList(HttpServletRequest request, HttpServletResponse response, Integer pageNum, Integer pageSize,
			Long pid) throws Exception {
		Page page = new Page();
		try {
			USession us = this.getUserSession(request);
			page = hostService.getHostList(us.getId(), pageNum, pageSize, pid);
		} catch (Exception e) {
			page.setCode(Const.FAIL_500);
			logger.error("", e);
		}
		return page;
	}

	@RequestMapping(value = "/getAllHostList")
	@ResponseBody
	public Result getAllHostList(HttpServletRequest request, HttpServletResponse response, Long pid) throws Exception {
		Result ret = new Result();
		try {
			USession us = this.getUserSession(request);
			List<Hosts> list = hostService.getHostAllList(us.getId(), pid);
			ret.setResult(Const.SUC, list);
		} catch (Exception e) {
			ret.setCode(Const.FAIL_500);
			logger.error("", e);
		}
		return ret;
	}

	@RequestMapping(value = "/saveHosts")
	@ResponseBody
	public Result saveHosts(HttpServletRequest request, HttpServletResponse response, String website, Long pid)
			throws Exception {
		Result ret = new Result();
		try {
			USession us = this.getUserSession(request);
			if (us == null) {
				ret.setResult(Const.FAIL, "请重新登陆");
				return ret;
			}

			String line = System.getProperty("line.separator");
			String[] array = website.split(line);
			if (array.length == 0) {
				ret.setResult(Const.FAIL, "请输入url");
				return ret;
			}

			if (array.length == 1) {
				array = website.split("\n");
				if (array.length == 1) {
					array = website.split("\r");
				}
			}
			HashMap<String, String> map = new HashMap<String, String>();
			for (String url : array) {

				if (!url.startsWith("http")) {
					url = "http://" + url;
				}
				java.net.URL u = new java.net.URL(url);
				String host = u.getHost();// 获取主机名
				String key = host;
				if (!map.containsKey(key)) {
					map.put(key, host);
				}

			}
			Set<String> keys = map.keySet();
			for (String key : keys) {
				if (hostService.getHostsByUrl(us.getId(), key) == null) {
					hostService.saveHosts(us.getId(), pid, key);
				}
			}
			ret.setResult(Const.SUC, "操作成功");

		} catch (Exception e) {
			ret.setCode(Const.FAIL_500);
			logger.error("", e);
		}
		return ret;
	}

}
